/**
 * Copyright (C) 2010 altuure <altuure [AT] gmail [DOT] com> http://www.altuure.com/projects/yagdao
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.altuure.yagdao.model;

import org.apache.commons.lang.builder.EqualsBuilder;
import org.apache.commons.lang.builder.HashCodeBuilder;
import org.apache.commons.lang.builder.ToStringBuilder;

import javax.persistence.*;
import java.io.Serializable;
import java.util.Date;

/**
 * Simple entity bean with no relation
 * @author makkan
 */
@Entity
@Table(name = "erp_simple")
@NamedQueries({ @NamedQuery(name = "namedQuery1", query = "select s from SimpleBean s where s.pint>:arg1"),
        @NamedQuery(name = "namedQuery2", query = "select s from SimpleBean s where s.pbyte>:arg1") })
public class SimpleBean implements Serializable {
    /**
     * 
     */
    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue
    public long id;
    @Column
    public String pstring;
    @Column
    public String pstringNullable;

    @Column
    public double pdouble;

    @Column
    public int pint;

    @Column
    public Date pdate;

    @Column
    public byte pbyte;
    @Column
    private double pint2;

    public SimpleBean() {
        super();

    }

    public static  SimpleBean randomBean() {
        SimpleBean bean = new SimpleBean();
        return randomBean(bean);
    }

    public static  SimpleBean randomBean(SimpleBean bean) {
        double r = Math.random() * 1000000d;
        long ONEMONTH = 1000L * 60L * 60L * 24L * 30L;
        // between 30 days earlier or after
        double rd = (Math.random() * 2L * ONEMONTH) - ONEMONTH;
        bean.pstring = String.valueOf("name-" + (int) r);

        bean.pdouble = r;
        {
            final double randomString = Math.random();
            if (randomString < 0.8)
                bean.pstringNullable = String.valueOf("allname-" + (int) r);
            else if (randomString<0.9)
                bean.pstringNullable = "";
            else
                bean.pstringNullable = null;
        }
        bean.pint = (int) r;
        bean.pint2 = (int) Math.random() * 1000000d;
        bean.pbyte = (byte) r;
        bean.pdate = new Date(System.currentTimeMillis() + (int) rd);
        return bean;
    }

    @Override
    public boolean equals(final Object other) {
        if (!(other instanceof SimpleBean))
            return false;
        SimpleBean castOther = (SimpleBean) other;
        return new EqualsBuilder().append(id, castOther.id).append(pstring, castOther.pstring)
                .append(pstringNullable, castOther.pstringNullable)
                .append(pdouble, castOther.pdouble).append(pint, castOther.pint).append(pdate, castOther.pdate)
                .append(pbyte, castOther.pbyte).append(pint2, castOther.pint2).isEquals();
    }

    @Override
    public int hashCode() {
        return new HashCodeBuilder().append(id).append(pstring).append(pstringNullable)
                .append(pdouble).append(pint).append(pint2)
                .append(pdate).append(pbyte).toHashCode();
    }

    @Override
    public String toString() {
        return new ToStringBuilder(this).append("id", id)
                .append("pstring", pstring)
                .append("pstringNullable", pstringNullable)
                .append("pdouble", pdouble)
                .append("pint", pint).append("pint2", pint2).append("pdate", pdate).append("pbyte", pbyte).toString();
    }

    public long getId() {
        return id;
    }

    public void setId(long id) {
        this.id = id;
    }

    public String getPstring() {
        return pstring;
    }

    public void setPstring(String pstring) {
        this.pstring = pstring;
    }

    public double getPdouble() {
        return pdouble;
    }

    public void setPdouble(double pdouble) {
        this.pdouble = pdouble;
    }

    public int getPint() {
        return pint;
    }

    public void setPint(int pint) {
        this.pint = pint;
    }

    public Date getPdate() {
        return pdate;
    }

    public void setPdate(Date pdate) {
        this.pdate = pdate;
    }

    public byte getPbyte() {
        return pbyte;
    }

    public void setPbyte(byte pbyte) {
        this.pbyte = pbyte;
    }

    public double getPint2() {
        return pint2;
    }

    public void setPint2(double pint2) {
        this.pint2 = pint2;
    }

    public String getPstringNullable() {
        return pstringNullable;
    }

    public void setPstringNullable(String pstringNullable) {
        this.pstringNullable = pstringNullable;
    }
}
